package com.codejiwei.core.graphx

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.graphx.{Graph, VertexId}
import org.apache.spark.graphx.util.GraphGenerators

object GraphX_Subgraph {
  def main(args: Array[String]): Unit = {

    val sc = new SparkContext(new SparkConf().setMaster("local[*]").setAppName("GraphXSubgraph"))
    val graph: Graph[VertexId, Int] = GraphGenerators.logNormalGraph(sc, numVertices = 10, numEParts = 5, mu = 2.0, sigma = 1.5)

    // 指定要包含的节点
    val includedVertices: Set[VertexId] = Set(1L, 3L, 5L)

    // 定义顶点谓词函数，保留包含在 includedVertices 中的顶点
    val vertexPredicate: (VertexId, VertexId) => Boolean = (v, d) => includedVertices.contains(v)

    // 提取子图，保留包含指定节点的子图
    val subgraph = graph.subgraph(vpred = vertexPredicate)

    println(subgraph.vertices.collect().mkString(", "))
    println(subgraph.edges.collect().mkString(", "))

  }
}
